﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class CAB50 :DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        CAB50Data m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataRow dwABPOS;
        public DataRow dwABKOPF;
        string OrderNumber="";
        public DataSet dsParamaters;
        
        #endregion
        public CAB50()
        {
            InitializeComponent();
            this.Name = "网络纱线生产单";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {

            dicParamater["blBuildCellMastrix"] = true;
            dicParamater["DocType"] = "CYARN";
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new CAB50Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy; 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                InitDataBinding();
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS,ABPART,ABPVAR," +
                " ABPSOLLMENGE5/100 AS WEI,ABPFARBE,ABPSTARTDAT,ABPENDDAT,'' as COMMENTS,'' as FACTORY,ABPLTVORL, "
                + " '' as USAGE,'' as FABRIC ,'' AS REMARK, '' as SBORDER,'' AS DIORDER ,'' AS ADDR"
               + " FROM INTEX.ABPOS WHERE  ABPAUFART='GP' AND ABPERLKZ <>'J' AND ABPAUFKRS LIKE 'C%' ";
                // +" and abpaufkrs = 'DB' and abpauflfd = '001213' ";


                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";

                    byte[] b;
                    string value;
                    DataColumn dc = new DataColumn();
                    dc.ColumnName = "ABPLTVORL";
                    foreach (DataRow dw in dsResult.Tables["ABPOS"].Rows)
                    {
                        //foreach (DataColumn dc in dsResult.Tables["ABPOS"].Columns)
                        {
                            if (dc.ColumnName == "ABPLTVORL")
                            {
                                b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                                value = Encoding.GetEncoding("gb2312").GetString(b);
                                dw[dc.ColumnName] = value;
                            }

                        }
                    }
                    
                }
                setUI.Merge(dsResult);
                setUI.AcceptChanges();
                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void InitDataBinding()
        {  
            this.setUI.Tables.Add("ABPOS");
            this.setUI.Tables.Add("GAAUF");
            orlReadCommand = OracleDB.CreateCommand();

            #region "ABPOS"
            orlReadCommand.CommandText = "SELECT Distinct ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS,ABPART,ABPVAR,"
                    + " ABPSOLLMENGE5,ABPFARBE,ABPSTARTDAT,ABPENDDAT,'' as COMMENTS,'' as FACTORY,ABPLTVORL, "
                    + " '' as USAGE,'' as FABRIC ,'' AS REMARK, '' as SBORDER,'' AS DIORDER ,'' AS ADDR"
                    + "  FROM INTEX.ABPOS WHERE  RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "ABPOS";
                setUI.Merge(dsResult);
            }
            #endregion

            #region "GAAUF"
            orlReadCommand.CommandText = "SELECT DISTINCT GAART, GAVAR,GAFARBE,GAKG1,GAKG3,"
                + " FROM INTEX.GAAUF WHERE RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "GAAUF"; 
                setUI.Merge(dsResult);
            }
            #endregion

            setUI.AcceptChanges();
            this.ABPOSDataGrid.DataContext = setUI;
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                ReloadABFOLGE(dwABPOS);
                ReloadABAUFM(dwABPOS);
                LoadComments(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }

        }
        private void LoadComments(DataRow dwABPOS)
        {
            try
            {
                this.txtABPAUFKRS.Text = dwABPOS["ABPAUFKRS"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPAUFLFD"].ToString();
                OrderNumber = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString();

                string YarnOrder = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString();
                dwABPOS["COMMENTS"] = m_Data.GetCommentsFromTXTIND("AB3", YarnOrder);
                string YarnOrder1 = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                dwABPOS["REMARK"] = m_Data.GetCommentsFromTXTIND("ABP", YarnOrder1);
                Tracer.Speak("注意："+dwABPOS["REMARK"].ToString());
                ReloadGAAUF(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }

        private void ReloadABFOLGE(DataRow dwABPOS)
        {
            setUI = m_Data.GetABFOLGE(setUI, dwABPOS);
            //this.listABFOLGE.DataContext = setUI;
        }
        private void ReloadABAUFM(DataRow dwABPOS)
        {
            setUI = m_Data.GetABAUFM(setUI, dwABPOS);
            //this.listABFOLGE.DataContext = setUI;
        }
        private void ReloadGAAUF(DataRow dwABPOS)
        {
            setUI = m_Data.GetGAAUF(setUI, dwABPOS);
            this.listGAAUF.DataContext = setUI;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        { 
            DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            dwTmp.ItemArray = dwABPOS.ItemArray;
            DataSet setRpt = new DataSet();
            setRpt = m_Data.BuildAB50Data(setRpt, dwABPOS);
            //dwABPOS.ItemArray = dwTmp.ItemArray;
            dicParamater["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
            dicParamater["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
            //dicParamater["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();

            m_DataX = new CAB50Data(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;  
            CAB50Report m_rpt = new CAB50Report(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.GetAB50Report("EXCEL");
        }
    }
}
